package com.smartthing.rest.test;


import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class test {

    public static void main(String[] args) {

        String topic        = "SmartThing/input/json";
//        String content      = "{\n" +
//                "\"hardwareId\": \"123\",\n" +
//                "\"type\":\"DeviceLocation\",\n" +
//                "\"request\": {\n" +
//                "\"latitude\": \"28.88\",\n" +
//                "\"longitude\": \"-88.39\",\n" +
//                "\"elevation\": \"80\",\n" +
//                "\"updateState\": true,\n" +
//                "\"eventDate\": \"2010-02-10T19:40:03.390Z\"\n" +
//                "}\n" +
//                "}";
        String content="{\n" +
                "\t\"hardwareId\": \"123\",\n" +
                "\t\"type\": \"DeviceTrack\",\n" +
                "\t\"request\": {\n" +
                "\t\t\"userid\": \"小天\",\n" +
                "\t\t\"phone\": \"13534043389\",\n" +
                "\t\t\"status\": \"0\",\n" +
                "\t\t\"operationmsg\": \"22\",\n" +
                "\t\t\"tipmsg\": \"2016-02-10T19:40:03.390Z\",\n" +
                "\t\t\"updateState\": true,\n" +
                "\t\t\"eventDate\": \"2016-02-10T19:40:03.390Z\"\n" +
                "\t}\n" +
                "}";
        /*String content2="{\n" +
                "\"hardwareId\": \"123-TEST-4567890\",\n" +
                "\"type\":\"DeviceAlert\",\n" +
                "\"request\": {\n" +
                "\"type\": \"engine.overheat\",\n" +
                "\"level\": \"Warning\",\n" +
                "\"message\": \"The engine is about to overheat! Turn the machine off!\",\n" +
                "\"updateState\": false,\n" +
                "\"eventDate\": \"2016-02-10T19:40:03.391Z\",\n" +
                "\"metadata\": { \n" +
                "\"name1\": \"value1\",\n" +
                "\"name2\": \"value2\"\n" +
                "}\n" +
                "}\n" +
                "}";
        String content3="{\n" +
                "\"hardwareId\": \"123-TEST-4567890\",\n" +
                "\"type\": \"DeviceMeasurements\",\n" +
                "\"request\": {\n" +
                "\"measurements\": { \n" +
                "\"fuel.level\": 87.1,\n" +
                "\"engine.temp\": 170\n" +
                "},\n" +
                "\"updateState\": true,\n" +
                "\"eventDate\": \"2016-02-10T19:40:03.391Z\"\n" +
                "}\n" +
                "}";*/
        /*String content4 = "{\n" +
                "\"hardwareId\": \"123-TEST-4567890\",\n" +
                "\"type\":\"DeviceLocation\",\n" +
                "\"request\": {\n" +
                "\"latitude\": \"33.75\",\n" +
                "\"longitude\": \"-84.39\",\n" +
                "\"elevation\": \"0\",\n" +
                "\"updateState\": true,\n" +
                "\"eventDate\": \"2016-02-10T19:40:03.390Z\"\n" +
                "}\n" +
                "}";*/
        int qos             = 2;
        String broker       = "tcp://10.12.8.222:1883";
        String clientId     = "JavaSample";
        MemoryPersistence persistence = new MemoryPersistence();

        try {
            MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
            MqttConnectOptions connOpts = new MqttConnectOptions();
            connOpts.setCleanSession(true);
            sampleClient.connect(connOpts);
            System.out.println("Publishing message: "+content);
            MqttMessage message = new MqttMessage(content.getBytes());
            message.setQos(qos);
            sampleClient.publish(topic, message);
            System.out.println("Message published");
            sampleClient.disconnect();
            System.out.println("Disconnected");
            System.exit(0);
        } catch(MqttException me) {
            System.out.println("reason "+me.getReasonCode());
            System.out.println("msg "+me.getMessage());
            System.out.println("loc "+me.getLocalizedMessage());
            System.out.println("cause "+me.getCause());
            System.out.println("excep "+me);
            me.printStackTrace();
        }
    }
}